<template>
  <div>
    <div>
      <a-modal
          :title="title"
          :width="800"
          :visible="visible"
          :confirmLoading="confirmLoading"
          @ok="handleOk"
          @cancel="handleCancel"
          cancelText="关闭"
          wrapClassName="ant-modal-cust-warp"
          style="top:5%;height: 85%;overflow-y: hidden">
        <a-spin :spinning="confirmLoading">
          <a-form-model ref="form" v-bind="layout" :model="model" :rules="rules">
            <a-form-model-item label="任务" required prop="job">
              <a-input v-model="model.job" :disabled="formDisabled" placeholder="请输入任务"/>
            </a-form-model-item>
            <a-form-model-item label="表达式" required prop="cron" :disabled="formDisabled">
              <a-input v-model="model.cron" placeholder="配置Cron表达式">
                <a-icon slot="addonAfter" type="setting" @click="handleShowCron"/>
              </a-input>
            </a-form-model-item>
            <a-form-model-item label="参数" required prop="params">
              <a-input v-model="model.params" :disabled="formDisabled" placeholder="请输入参数"/>
            </a-form-model-item>
            <a-form-model-item label="状态" required prop="status">
              <a-select v-model="model.status" :disabled="formDisabled" placeholder="请选择状态">
                <a-select-option value="1">正常</a-select-option>
                <a-select-option value="2">失效</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-form-model>
        </a-spin>
      </a-modal>
    </div>
    <div>
      <a-modal
          :footer="null"
          :closable="false"
          title="Cron表达式生成器" width="60%" :visible="openCron" class="scrollbar">
        <crontab @hide="openCron=false" @fill="crontabFill" :expression="expression"></crontab>
      </a-modal>
    </div>
  </div>
</template>

<script>
import {save} from '@/services/quartz'
import {list as userList} from '@/services/user'
import Crontab from '@/components/crontab'

export default {
  name: "QuartzForm",
  components: {Crontab},
  data() {
    return {
      closeProps: true,
      expression: '',
      openCron: false,
      title: "操作",
      visible: false,
      formDisabled: false,
      isOkDisabled: false,
      model: {},
      userOptions: [],
      layout: {
        labelCol: {span: 3},
        wrapperCol: {span: 20},
      },
      confirmLoading: false,
      rules: {
        title: [
          {required: true, message: '请输入消息内容!'},
        ],
      },
    }
  },
  mounted() {
    this.initDict()
  },
  methods: {
    add() {
      this.title = "添加消息通知";
      this.model = {};
      this.formDisabled = false
      this.isOkDisabled = false
      this.visible = true;
    },
    edit(record) {
      this.title = "编辑消息通知";
      this.model = Object.assign({}, record);
      this.formDisabled = false
      this.isOkDisabled = false
      this.visible = true;
    },
    view(record) {
      this.title = "查看消息通知";
      this.model = Object.assign({}, record);
      this.formDisabled = true
      this.isOkDisabled = true
      this.visible = true;
    },
    submit() {
      if (this.isOkDisabled) {
        this.visible = false;
      } else {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.confirmLoading = true;
            save(this.model).then(res => {
              if (res.data.code === 200) {
                this.$message.success('操作成功' || res.data.msg);
                this.$emit('refresh');
              } else {
                this.$message.error('操作失败' || res.data.msg);
              }
            }).catch(error => {
              this.$message.error('操作失败' || error.message);
            }).finally(() => {
              this.confirmLoading = false;
            })
            this.visible = false;
          }
        })
      }
    },
    close() {
      this.$refs.form.clearValidate();
      this.$emit('close');
      this.visible = false;
      this.model = {}
    },
    handleOk() {
      this.submit()
    },
    handleCancel() {
      this.close()
    },
    initDict() {
      userList().then(res => {
        this.userOptions = res.data.data || []
      })
    },
    /** 确定后回传值 */
    crontabFill(value) {
      this.model.cron = value
    },
    /** cron表达式按钮操作 */
    handleShowCron() {
      this.expression = this.model.cron
      this.openCron = true
    },
  }
}
</script>

<style scoped>

</style>
